<template>
	<div class="openClass">
		<div class="u_resourceH classH clear">
			<div class="u_resourceH_l fl">
				<template v-if="!$store.state.isSchool">
					<el-select filterable placeholder="请选择" v-model="subjectId" @change="changeSelect">
						<el-option v-for="item in subjectList" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</template>
			</div>
			<!--  -->
			<div class="u_resourceH_r fr">
				<el-input size="medium" placeholder="专辑名称" suffix-icon="el-icon-search" v-model="keywords" @change="changInputName">
				</el-input>
			</div>
		</div>
		<!--  -->
		<div class="classBox" v-loading="loading">
			<ul>
				<li class="classLs clear" v-for="(item, index) in favouriteList" :key="item.id">
					<a target="_blank" @click="openAlbum(item)">
						<img :src="BASE_URL + item.picture" :onerror="errorImg">
					</a>
					<div class="classLs_info">
						<div class="classLs_1 clear">
							<a target="_blank" @click="openAlbum(item)">{{ item.res_name }}</a>
							<div class="resource_ioperation fr">
								<el-popover placement="bottom" width="" popper-class="ioperation_hover" trigger="hover">
									<ul class="r_ioperation">
										<li @click="cancelFav(item.id)">取消收藏</li>
									</ul>
									<i slot="reference" class="el-icon-arrow-down"></i>
								</el-popover>
							</div>
						</div>
						<div class="classLs_2">
							<strong>{{ item.courses }}门课程</strong>
							<span v-for="keyword in item.keyword" :key="keyword">{{keyword}}</span>
						</div>
						<div class="classLs_3" v-if="item.profile != ''">
							<el-tooltip class="item" effect="dark" :content="item.profile" placement="top-start">
								<p>{{item.profile}}</p>
							</el-tooltip>
						</div>
					</div>
				</li>
			</ul>
		</div>
		<div class="pageing" v-if=" total_number >= page_size">
			<el-pagination @current-change="change_page" :current-page="page_index" :page-size="page_size" layout="prev, pager, next" :total="total_number">
			</el-pagination>
		</div>
		<div class="dependent" v-if="placeholder">
			<i class="ipm image_nofound"></i>
			<p>还没发现任何相关的内容哦~</p>
		</div>
	</div>
</template>
<script>
export default {
	name: '',
	data() {
		return {
			errorImg: 'this.src="' + require('../../../public/images/image20.png') + '"',
			origin: 3,
			subjectId: 0, //学科
			subjectList: this.$g.subjectList, //学科表
			keywords: '', //资源名称
			page_index: 1,
			page_size: this.$g.page_size,
			favouriteList: [],
			total_number: 0,
			placeholder: false,
			loading: true,
		}
	},
	created() {
		this.getFavourite()
	},
	methods: {
		/*
			跳转到优课专辑模块查看视频
		 */
		openAlbum(item) {

			let host = sessionStorage.getItem('host')
			let redirectParams = sessionStorage.getItem('redirect_params')
			let link = `${this.BASE_URL}${redirectParams}&host=${host}&routerUrl=/home/albumDetail&id=${item.id}`
			window.open(link)
		},
		getFavourite() {
			let success = (res => {
				var data = res.data;
				this.favouriteList = data.data;
				this.total_number = data.data_sum
				this.page_index = parseInt(data.page_index)
				this.subjectList = this.$g.subjectList
				this.favouriteList.forEach((item, index) => {
					item.keyword = JSON.parse(item.keyword)
				})

				this.loading = false
				this.placeholder = this.favouriteList.length === 0 ? true : false

			})
			this.$ajax({
				url: '/api/favourite/favourite',
				params: {
					user_id: this.$g.user_id,
					origin: this.origin,
					page: this.page_index,
					size: this.page_size,
					data_type: 2,
					keyword: this.keywords,
					subject_id: this.subjectId
				},
				func: { success: success }
			})
		},
		change_page(val) {
			this.page_index = val
			this.getFavourite();
		},
		changInputName() {
			this.page_index = 1
			this.getFavourite();
		},
		changeSelect(val) {
			this.page_index = 1
			this.getFavourite();
		},
		cancelFav(res_id) {
			this.$confirm('此操作是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let success = (res => {
					this.$message.success(res.msg)
					this.page_index = 1
					this.getFavourite();
				})
				this.$ajax({
					url: '/api/favourite/favourite',
					method: 'delete',
					params: {
						user_id: this.$g.user_id,
						origin: this.origin,
						res_id: res_id
					},
					func: { success: success }
				})
			}).catch(() => {});
		}
	}
}
</script>